﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Noris.Tools.TraceVisualiser.Components
{
	[DefaultEvent("OkClick")]
	internal class OkStornoPanel : DPanel
	{
		public OkStornoPanel()
		{
			this._Init();
		}
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public Button OkButton { get { return this.OkBtn; } }
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public Button StornoButton { get { return this.StornoBtn; } }
		[Description("Událost po kliknutí na tlačítko OK")]
		[Category("OkStornoPanel")]
		public event EventHandler OkClick;
		[Description("Událost po kliknutí na tlačítko Storno")]
		[Category("OkStornoPanel")]
		public event EventHandler StornoClick;
        /// <summary>
        /// Povolí chování Enter = Form.Accept()
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("true = Povolí chování Enter = Form.Accept()")]
        [Category("OkStornoPanel")]
        public bool AcceptFormEnable
        {
            get { return this._AcceptFormEnable; }
            set { this._SetAcceptFormEnable(value); }
        }
        /// <summary>
        /// Povolí chování Escape = Form.Close()
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Description("true = Povolí chování Escape = Form.Close()")]
        [Category("OkStornoPanel")]
        public bool CancelFormEnable
        {
            get { return this._CancelFormEnable; }
            set { this._SetCancelFormEnable(value); }
        }
        private void _SetAcceptFormEnable(bool value)
        {
            this._AcceptFormEnable = value;
            Form form = this.FindForm();
            if (form != null)
            {
                if (value)
                    form.AcceptButton = this.OkBtn;
                else
                    form.AcceptButton = null;
            }
        }
        private void _SetCancelFormEnable(bool value)
		{
			this._CancelFormEnable = value;
			Form form = this.FindForm();
			if (form != null)
			{
				if (value)
					form.CancelButton = this.StornoBtn;
				else
					form.CancelButton = null;
			}
		}
        private bool _AcceptFormEnable;
        private bool _CancelFormEnable;
        /// <summary>
		/// Mezera mezi buttony
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Description("Mezera mezi buttony")]
		[Category("OkStornoPanel")]
		public int Spacing
		{
			get { return this._Spacing; }
			set { this._SetSpacing(value); }
		}
		private int _Spacing = 2;
		private void _SetSpacing(int spacing)
		{
			int spac = (spacing < 0 ? 0 : (spacing > 100 ? 100 : spacing));
			if (spac != this._Spacing)
			{
				this._Spacing = spac;
				this._ArrangeButtons();
			}
		}
		private Button OkBtn;
		private Button StornoBtn;
        /// <summary>
        /// Inicializace komponenty
        /// </summary>
		private void _Init()
		{
			this.OkBtn = new System.Windows.Forms.Button();
			this.OkBtn.Location = new System.Drawing.Point(0, 0);
			this.OkBtn.Name = "OkBtn";
			this.OkBtn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.OkBtn.TextImageRelation = TextImageRelation.ImageBeforeText;
			this.OkBtn.Size = new System.Drawing.Size(124, 35);
			this.OkBtn.TabIndex = 0;
			this.OkBtn.Text = "OK";
			this.OkBtn.UseVisualStyleBackColor = true;
			this.OkBtn.Click += new EventHandler(OkBtn_Click);

			this.StornoBtn = new System.Windows.Forms.Button();
			this.StornoBtn.Location = new System.Drawing.Point(130, 0);
			this.StornoBtn.Name = "StornoBtn";
			this.StornoBtn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.StornoBtn.TextImageRelation = TextImageRelation.ImageBeforeText;
			this.StornoBtn.Size = new System.Drawing.Size(124, 35);
			this.StornoBtn.TabIndex = 1;
			this.StornoBtn.Text = "Storno";
			this.StornoBtn.UseVisualStyleBackColor = true;
			this.StornoBtn.Click += new EventHandler(StornoBtn_Click);

			this.Anchor = (AnchorStyles)(AnchorStyles.Bottom | AnchorStyles.Right);
			this.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.Controls.Add(this.StornoBtn);
			this.Controls.Add(this.OkBtn);
			this.Name = "OkStornoPanel";
			this.Size = new System.Drawing.Size(255, 35);
			this._ArrangeButtons();

            this.ParentChanged += new EventHandler(OkStornoPanel_ParentChanged);
			this.SizeChanged += new EventHandler(OkStornoPanel_SizeChanged);

		}

		void OkStornoPanel_SizeChanged(object sender, EventArgs e)
		{
			this._ArrangeButtons();
		}
		private void _ArrangeButtons()
		{
			int s = this._Spacing;
			int w = this.Width;
			int h = this.Height;
			bool horizontal = (w > (3 * h));             // true = vodorovně vedle sebe  /  false = svisle pod sebou
			int bw = (horizontal ? ((w - s) / 2) : w);   // button width 
			int bh = (horizontal ? h : ((h - s) / 2));   // button height
			int ax = (horizontal ? bw + s : 0);          // add pixel to X pro druhý button
			int ay = (horizontal ? 0 : bh + s);          // add pixel to Y pro druhý button
			
			// Umístit buttony:
			this.OkBtn.Bounds = new System.Drawing.Rectangle(0, 0, bw, bh);
			this.StornoBtn.Bounds = new System.Drawing.Rectangle(ax, ay, bw, bh);

			// Vložit Image podle výšky:
			if (bh > 30)
			{
				this.OkBtn.Image = TraceVisualiser.Pics.PicLibrary22.DialogOkApply3_FromFile;
				this.StornoBtn.Image = TraceVisualiser.Pics.PicLibrary22.DialogCancel3_FromFile;
			}
			else
			{
				this.OkBtn.Image = TraceVisualiser.Pics.PicLibrary16.DialogOkApply3_FromFile;
				this.StornoBtn.Image = TraceVisualiser.Pics.PicLibrary16.DialogCancel3_FromFile;
			}
		}
        /// <summary>
        /// Po změně parenta
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OkStornoPanel_ParentChanged(object sender, EventArgs e)
        {
            this._CheckCancelButton();
        }
        /// <summary>
        /// Zajistí, že pokud je povoleno CancelFormEnable, tak na Formu bude jeho property CancelButton nastavena na zdejší button StornoBtn.
        /// </summary>
        private void _CheckCancelButton()
        {
            if (!this._CancelFormEnable) return;

            Form form = this.FindForm();
            if (form != null)
            {
                if (form.CancelButton != this.StornoBtn)
                    form.CancelButton = this.StornoBtn;
            }
        }
		void OkBtn_Click(object sender, EventArgs e)
		{
			if (this.OkClick != null)
				this.OkClick(this, e);
            else if (this._AcceptFormEnable)
            {
                Form form = this.FindForm();
                if (form != null)
                {
                    form.DialogResult = DialogResult.OK;
                    form.Close();
                }
            }
		}
		void StornoBtn_Click(object sender, EventArgs e)
		{
			if (this.StornoClick != null)
				this.StornoClick(this, e);
            else if (this._CancelFormEnable)
            {
                Form form = this.FindForm();
				if (form != null)
				{
					form.DialogResult = DialogResult.Cancel;
					form.Close();
				}
            }
		}
	}
}
